//
//  ManViewController.m
//  Findif
//
//  Created by Mac on 2019/4/9.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "ManViewController.h"
#import "MyDiscountTableViewCell.h"
#import "ShopDetailsViewController.h"

@interface ManViewController ()<UITableViewDelegate,UITableViewDataSource,MyDiscountTableViewCellDelegate>

@property (nonatomic, weak)UITableView * tableView;

@end

@implementation ManViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"满减券";
    self.view.backgroundColor = ZRWhiteColor;
    
    [self setupUI];
}

- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ZRWhiteColor;
    //    tableView.rowHeight = 110 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView = tableView;
    
    
    [self.tableView registerClass:[MyDiscountTableViewCell class] forCellReuseIdentifier:@"MyManDiscountCell"];
    
    //    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    //
    //        [self updateData];
    //    }];
    
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyDiscountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyManDiscountCell" forIndexPath:indexPath];
    
    cell.userModel = self.modelArray[indexPath.row];
    cell.delegate = self;

    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 使用缓存行高，避免计算多次
    SureUserCouponModel * model = self.modelArray[indexPath.row];
    return model.rowHeight;
    
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    SureUserCouponModel * model = self.modelArray[indexPath.row];
//    NSString * shop_id = [NSString stringWithFormat:@"%@",model.merchants_id];
//
//    if ([shop_id isEqualToString:@"0"]) {
//
//        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
//                                                                       message:@"该券为全场通用券，在任何一家店铺都能使用"
//                                                                preferredStyle:UIAlertControllerStyleAlert];
//
//        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
//                                                              handler:^(UIAlertAction * action) {
//
//                                                              }];
//
//        [alert addAction:defaultAction];
//        [self presentViewController:alert animated:YES completion:nil];
//    }else{
//
//        HiddenBar;
//        ShopDetailsViewController * detailsVC = [ShopDetailsViewController new];
//        detailsVC.ID = shop_id;
//        [self.navigationController pushViewController:detailsVC animated:YES];
//        ShowBar;
//    }
//
//}

- (void)clickUnfoldButton{
    
    
    [self.tableView reloadData];
}

- (void)clickUseButtonWithCell:(MyDiscountTableViewCell *)cell{
    
    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
    
    SureUserCouponModel * model = self.modelArray[indexPath.row];
    NSString * shop_id = [NSString stringWithFormat:@"%@",model.merchants_id];
    
    if ([shop_id isEqualToString:@"0"]) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"该券为全场通用券，在任何一家店铺都能使用"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        
        HiddenBar;
        ShopDetailsViewController * detailsVC = [ShopDetailsViewController new];
        detailsVC.ID = shop_id;
        [self.navigationController pushViewController:detailsVC animated:YES];
        ShowBar;
    }
    
}


@end
